*sh submit_do.sh TFS_construction &


cap net install reghdfe, from(http://fmwww.bc.edu/repec/bocode/r)
cap net install coefplot, from(http://fmwww.bc.edu/repec/bocode/c)
cap net install outreg2, from(http://fmwww.bc.edu/repec/bocode/o)
cap net install tuples, from(http://fmwww.bc.edu/repec/bocode/t)
cap net install distinct, from(http://fmwww.bc.edu/repec/bocode/d)
cap net install tolower, from(http://fmwww.bc.edu/repec/bocode/t)

clear
do TFS_unitid.do

clear
use "3 CHOICE.dta"

*Keep years 1982 and forward because this is when home zip is available

keep if YEAR>=1982
tolower
save "3 CHOICE_YearRestrict.dta", replace

clear
use "1 DEMOGRAPHICS.dta"
keep if YEAR>=1982
tolower

merge 1:1 subjid year acerecode using "3 CHOICE_YearRestrict.dta"
rename _merge mergechoicedemog

tostring homezip, gen(homezipstr)
replace homezipstr = "0" + homezipstr if strlen(homezipstr)==4

save choice_demog.dta, replace


erase "3 CHOICE_YearRestrict.dta"


*Merge to counties

do zipcrosswalk.do

do missinginstcty.do

clear
use "2 HIGH SCHOOL.dta"
keep if YEAR>=1982
tolower
keep subjid year acerecode satv satm actcomp hsgpa hsgpa07_n hsrank02 rate01 rate04 rate07 rate08 rate10 rate11

merge 1:1 subjid year acerecode using "choicedemog_addcty.dta"
rename _merge mergechoice
keep if mergechoice==3

save "hs_choice_demog_merge_normasylum.dta", replace
erase choicedemog_addcty.dta

clear

use "4 PLANS.dta"
keep if YEAR>=1982
tolower
keep subjid year acerecode planlive preflive degasp scareera majora futact12 futact24 futact32 futact33 futact08_t futact06_t goal05 goal19  futact04 futact05 

merge 1:1 subjid year acerecode using "hs_choice_demog_merge_normasylum.dta"
rename _merge mergeplans
keep if mergeplans==3

save "hs_choice_demog_plans_normasylum.dta", replace
erase "hs_choice_demog_merge_normasylum.dta"
clear

use "6 FUNDS.dta"
keep if YEAR>=1982
tolower

#delimit ;
keep subjid year acerecode fincon aid1 aid2 aid3 aid4 aid5 aid01 aid02 aid03 aid04 aid05 aid06 aid07 aid08 aid09 aid10 aid11 aid12 aid13 
aid14 aid15 aid16 aid17 aid18 aid19 aid20 aidothcol82_n;

#delimit cr
merge 1:1 subjid year acerecode using "hs_choice_demog_plans_normasylum.dta"
rename _merge mergefunds
keep if mergefunds==3

save "hs_ch_demog_pl_funds_normasylum.dta", replace
erase "hs_choice_demog_plans_normasylum.dta"
clear

use "5 VIEW.dta"
keep if YEAR>=1982
tolower

#delimit ;
keep subjid year acerecode poliview;

#delimit cr
merge 1:1 subjid year acerecode using "hs_ch_demog_pl_funds_normasylum.dta"
rename _merge mergeviews
keep if mergeviews==3

save "hs_ch_demog_pl_funds_v_normasylum.dta", replace
erase "hs_ch_demog_pl_funds_normasylum.dta"

local vars motheduc fatheduc satm satv sex fullstat
foreach x of local vars{
gen `x'_pub = `x'
drop `x'
gen `x' = `x'_pub
replace `x' = . if `x'_pub==0
}


save "hs_ch_demog_pl_funds_v_nodup.dta", replace

clear
use "hs_ch_demog_pl_funds_v_nodup.dta"
gen dist10orless = disthome==1
replace dist10orless = . if disthome==.

gen dist11to50 = disthome==2
replace dist11to50 = . if disthome==.

gen dist51to100 = disthome==3
replace dist51to100 = . if disthome==.

gen dist101to500 = disthome==4
replace dist101to500 = . if disthome==.

gen distgt500 = disthome==5
replace distgt500 = . if disthome==.

drop homectyfipsstr homestate

tostring homectyfips, gen(homectyfipsstr)
replace homectyfipsstr = "0" + homectyfipsstr if strlen(homectyfipsstr)==4
gen homestate = substr(homectyfipsstr, 1, 2)
destring homestate, replace

gen attendnormincty = prevnormsch==1 & homeinstsamecty==1
replace attendnormincty = . if homeinstsamecty==.|prevnormsch==.

gen attendnormwithin10 = prevnorm==1 & dist10orless==1
replace attendnormwithin10 = . if dist10orless==.
replace attendnormwithin10 = . if prevnormsch==.

replace maxhomezipsameasinstzip = . if instzip==""

tab disthome if maxhomezipsameasinstzip==1 & home_hasnormalschool~=.
tab disthome if homeinstsamecty ==1 & home_hasnormalschool~=.
tab disthome 

tab disthome if maxhomezipsameasinstzip==1 & home_hasnormalschool~=. & studwgt~=0 & studwgt~=.
tab disthome if homeinstsamecty ==1 & home_hasnormalschool~=. & studwgt~=0 & studwgt~=.
tab disthome if studwgt~=0 & studwgt~=.

#delimit ;

su satv satm actcomp acerecode homezipstr fatheduc motheduc disthome choice choose09 choose29 reason05 reason06 reason07 sex fullstat 
 citizen hsgpa hsrank02 age2 racegroup parborn income degasp oberegion if mergepubaceyear==3;

#delimit cr
local vars motheduc_pub fatheduc_pub satm_pub satv_pub sex_pub fullstat_pub
foreach x of local vars{
tab `x' if mergepubaceyear==3, missing nolab
}
count

save "TFS_pub_ipeds_merge_12Dec.dta", replace

do TFS_univresponse.do
